<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 xsi exsl">

		
	<!-- 医嘱章节 -->
	<xsl:template match="*" mode="ProviderRabOrdersSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_ProviderOrders]" mode="ProviderRabOrdersEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 医嘱章节条目 (仅处理检查医嘱条目) -->
	<xsl:template match="*" mode="ProviderRabOrdersEntry">
		<xsl:apply-templates select="hl7:entry/hl7:organizer[(hl7:component/hl7:observation/hl7:code/@code='DE06.00.289.00') and (hl7:component/hl7:observation/hl7:value/@code='02')]" mode="HCCRadOrder">
			<xsl:with-param name="sectionType" select="'POR'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	 
	<!-- 体格检查章节 (仅处理 C0006《检查报告》) -->
	<xsl:template match="*" mode="RadPhysicalExaminationSection">
		<xsl:if test="$hccDocType='C0006'">
			<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_PhysicalExamination]" mode="RadPhysicalExaminationEntry">
			</xsl:apply-templates>
		</xsl:if >
	</xsl:template>
	<!-- 体格检查章节条目 -->
	<xsl:template match="*" mode="RadPhysicalExaminationEntry">
		    <xsl:apply-templates select="hl7:entry[hl7:organizer/hl7:component/hl7:observation/hl7:code/@code='DE04.30.019.00']" mode="HCCRadOrder">
			<xsl:with-param name="sectionType" select="'RPE'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	
	<!-- 辅助检查章节 -->
	<xsl:template match="*" mode="RadSupplementaryExamSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_SupplementaryExam]" mode="RadSupplementaryExamEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 辅助检查章节条目 -->
	<xsl:template match="*" mode="RadSupplementaryExamEntry">
		<xsl:apply-templates select="hl7:entry" mode="HCCRadOrder">
			<xsl:with-param name="sectionType" select="'RSE'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	
	<!--通用检查医嘱条目输出模板-->
	<xsl:template match="*" mode="HCCRadOrder">
		<xsl:param name = "sectionType" />
		<xsl:variable name="externalRadOrderNo" select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.24']/@extension"/>
		<RadOrder>
		<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
		<xsl:choose>
			<xsl:when test="$externalRadOrderNo !=''">
				<ExternalId><xsl:value-of select="concat($externalRadOrderNo,'-',position())"/></ExternalId>
			</xsl:when>
			<xsl:otherwise>
				<ExternalId><xsl:value-of select="concat($hccDocNo,'-',$sectionType,'-',position())"/></ExternalId>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:choose>
			<!-- 医嘱章节 -->
			<xsl:when test="$sectionType='POR'">
					<OrderItem>
						<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:value"/></Code>
						<Description><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:value"/></Description>
					</OrderItem>
					<OrderQuantity><xsl:value-of select="1"/></OrderQuantity>
					<FromTime>
					<xsl:value-of select="isc:evaluate('xmltimestamp',hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:effectiveTime/hl7:high/@value)"/></FromTime>
					<ToTime>
					<xsl:value-of select="isc:evaluate('xmltimestamp',hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:effectiveTime/hl7:low/@value)"/></ToTime>
					<Status>
					<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.290.00']/hl7:value"/>
					</Status>
					<OrderedBy>
						<Name><GivenName>
						<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:author/hl7:assignedAuthor/hl7:assignedPerson/hl7:name"/>
						</GivenName></Name>
						<Code>
						<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:author/hl7:assignedAuthor/hl7:id/@extension"/>
						</Code>
					</OrderedBy>
					<VerifiedBy>
						<Name><GivenName>
						<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:participant/hl7:participantRole[hl7:code/@displayName='医嘱审核人']/hl7:playingEntity/hl7:name"/>
						</GivenName></Name>
						<Code>
						<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:participant/hl7:participantRole[hl7:code/@displayName='医嘱审核人']/hl7:id/@extension"/>
						</Code>
					</VerifiedBy>
					<Comments>
					<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.179.00']/hl7:value" />
					</Comments>
					<OrderCategory>
						<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.289.00']/hl7:value/@code"/></Code>
						<Description><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.289.00']/hl7:value/@displayName"/></Description>
					</OrderCategory>
				</xsl:when>

			<!-- 辅助检查章节 -->
			<xsl:when test="$sectionType='RSE'">
					<OrderItem>
						<Code><xsl:value-of select="'辅助检查'"/></Code>
						<Description><xsl:value-of select="'辅助检查'"/></Description>
					</OrderItem>
					<OrderQuantity><xsl:value-of select="1"/></OrderQuantity>
					<Specimen><xsl:value-of select="'未知'"/></Specimen>
					<Result>
						<ResultText>
							<xsl:value-of select="hl7:observation/hl7:value"/>
						</ResultText>
					</Result>
				</xsl:when>

			<!-- 体格检查章节 -->
			<xsl:when test="$sectionType='RPE'">				
				<OrderItem>
					<Code><xsl:value-of select="hl7:organizer/hl7:component[hl7:observation/hl7:code/@code='DE04.30.019.00']/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:value"/></Code>
					<Description><xsl:value-of select="hl7:organizer/hl7:component[hl7:observation/hl7:code/@code='DE04.30.019.00']/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:value"/></Description>
				</OrderItem>
				<FromTime>
					<xsl:value-of select="isc:evaluate('xmltimestamp',hl7:organizer/hl7:component[hl7:observation/hl7:code/@code='DE04.30.019.00']/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:effectiveTime/@value)"/>
				</FromTime>
				<OrderQuantity><xsl:value-of select="1"/></OrderQuantity>
				<Specimen><xsl:value-of select="'未知'"/></Specimen>
				<OrderCategory>
					<Code>
						<xsl:value-of select="../hl7:entry[hl7:observation/hl7:code/@code='DE04.30.018.00']/hl7:observation/hl7:value"/>
					</Code>
					<Description>
						<xsl:value-of select="../hl7:entry[hl7:observation/hl7:code/@code='DE04.30.018.00']/hl7:observation/hl7:value"/>
					</Description>
				</OrderCategory>
				<Result>
					<!-- 从检查报告章节获取检查结果 -->
					<ResultText>
						<xsl:value-of select="../../../hl7:component[hl7:section/hl7:code/@displayName='检查报告']/hl7:section/hl7:entry[hl7:observation/hl7:code/@code='DE04.50.131.00']/hl7:observation/hl7:value"/>
						<!--<xsl:value-of select="../../../hl7:component[hl7:section/hl7:code/@displayName='检查报告']/hl7:section/hl7:entry[1]/hl7:observation/hl7:value"/>-->
					</ResultText>
				</Result>					
			</xsl:when>			
		</xsl:choose>
		
		</RadOrder>		
	</xsl:template>
	
</xsl:stylesheet>
